package ipebg.mzk.provider.system.service.impl;

import ipebg.mzk.facade.entity.MsFunctionOption;
import ipebg.mzk.facade.entity.MsFunctionRoleOption;
import ipebg.mzk.facade.system.service.MsFunctionOptionService;
import ipebg.mzk.provider.system.dao.MsFunctionOptionDao;
import ipebg.mzk.provider.system.dao.MsFunctionRoleOptionDao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("msFunctionOptionServiceImpl")
public class MsFunctionOptionServiceImpl implements MsFunctionOptionService{
	
	@Resource(name="msFunctionOptionDao")
	MsFunctionOptionDao msFunctionOptionDao;
	
	@Resource(name="msFunctionRoleOptionDao")
	MsFunctionRoleOptionDao functionRoleOptionDao;

	@SuppressWarnings("unchecked")
	@Override
	public List<MsFunctionOption> findAllOptionByFunc(String nodeCode) throws Exception {
		List<MsFunctionOption> list=null;
		String selectSql="";
		try{
			selectSql="from MsFunctionOption where functionId='"+nodeCode+"'";
			list=msFunctionOptionDao.queryForList(selectSql, null);
		}catch (Exception e) {
			throw e;
		}
		return list;
	}

	@Override
	public void save(MsFunctionOption optionBean) throws Exception {		
		try {
			msFunctionOptionDao.saveOrUpdate(optionBean);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public MsFunctionOption findOption(String functionId, String optionId) throws Exception {
		MsFunctionOption bean=null;
		String selectSql="";
		try{
			selectSql="from MsFunctionOption where functionId='"+functionId+"' and optionId='"+optionId+"'";
			bean=msFunctionOptionDao.findUnique(MsFunctionOption.class,selectSql);
		}catch (Exception e) {
			throw e;
		}
		return bean;
	}

	@Override
	public void deleteOption(MsFunctionOption option) throws Exception {	
		try {
			msFunctionOptionDao.delete(option);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MsFunctionRoleOption> findFunctionRoleOptionListByOptionId(String optionId) throws Exception {
		List<MsFunctionRoleOption> resultList = null;
		String hql="from MsFunctionRoleOption fo where fo.optionId = '"+optionId+"'";		
		try {
			resultList = functionRoleOptionDao.queryForList(hql, null);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return resultList;
	}

	@Transactional
	public List<MsFunctionOption> findOptionByLoginNameFunctionId(String loginName,String functionId) {
		String sql = " select fo from MsAdminUser u,MsAdminUserRole ur,MsAdminRole r, " +
				" MsAdminFunction f,MsFunctionOption fo," +
				" MsFunctionRoleOption fro " +
				" where  u.loginName=? and f.functionId=? and u.adminId = ur.adminId and r.roleId = ur.roleId " +
				" and f.functionId=fo.functionId " +
				" and fro.optionId = fo.optionId and fro.roleId = r.roleId and fro.functionId=f.functionId ";
		return msFunctionOptionDao.queryForList(sql, new Object[]{loginName, functionId});
	}
}